package com.atguigu.lease.web.admin.controller.apartment;

import com.atguigu.lease.common.result.Result;
import com.atguigu.lease.model.entity.ApartmentInfo;
import com.atguigu.lease.model.enums.ReleaseStatus;
import com.atguigu.lease.web.admin.service.ApartmentInfoService;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentDetailVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentQueryVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentSubmitVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by 小可乐$ on 2024/8/13$ 13:58$
 * description: 公寓信息管理
 */
@Tag(name = "公寓信息管理")
@RestController
@RequestMapping("admin/apartment")
public class ApartmentController {
    @Autowired
    private ApartmentInfoService service;

    /**
     *根据id修改公寓的发布状态
     */
    @PostMapping("/updateReleaseStatusById")
    @Operation(summary = "根据id修改给公寓发布状态")
    public Result updateReleaseStatusById(
            @RequestParam Long id,
            @RequestParam ReleaseStatus status) {
//        new lambdaUpdate对象
        LambdaUpdateWrapper<ApartmentInfo> apartmentUpdateWrapper = new LambdaUpdateWrapper<>();
//        写判断条件
        apartmentUpdateWrapper.eq(ApartmentInfo::getId,id);
//        更新需要修改的值
        apartmentUpdateWrapper.set(ApartmentInfo::getIsRelease,status);
        service.update(apartmentUpdateWrapper);
        return Result.ok();
    }
    /**
     * 保存或更新公寓信息
     * */
    @PostMapping("saveOrUpdate")
    @Operation(summary = "保存或更新公寓信息")
    public Result saveOrUpdateApartment(@RequestBody ApartmentSubmitVo apartmentSubmitVo){
        service.saveOrUpdateApartment(apartmentSubmitVo);
        return Result.ok();
    }
    /**
     * 根据条件分页查询公寓列表
     * */
    @GetMapping("pageItem")
    @Operation(summary = "根据条件分页查询公寓列表")
    public Result<IPage<ApartmentItemVo>> iPageResult(@RequestParam Integer current,
                                                      @RequestParam Integer size,
                                                     ApartmentQueryVo apartmentQueryVo){
        Page<ApartmentItemVo> page = new Page<>(current,size);
        IPage<ApartmentItemVo> result =  service.iPageResult(page,apartmentQueryVo);
        return Result.ok(result);
    }
    /**
     * 根据id查询公寓详细信息
     * */
    @GetMapping("admin/apartment")
    @Operation(summary = "根据id查询公寓详细信息")
    public Result<ApartmentDetailVo> getApartmentDetail(@RequestParam Long id){
        ApartmentDetailVo apartmentDetail = service.getApartmentDetail(id);
        return Result.ok(apartmentDetail);
    }

    /**
     * 根据区县id查询公寓信息列表
     * */
    @GetMapping("listInfoByDistrictId")
    @Operation(summary = "根据区县id查询公寓信息列表")
    public Result<List<ApartmentInfo>> selectApartmentByDistrictId(@RequestParam Long id){
        LambdaQueryWrapper<ApartmentInfo> apartmentQueryWrapper = new LambdaQueryWrapper<>();
        apartmentQueryWrapper.eq(ApartmentInfo::getDistrictId,id);
        List<ApartmentInfo> list = service.list(apartmentQueryWrapper);
        return Result.ok(list);
    }
    /**
     * 根据id删除公寓信息
     * */
    @DeleteMapping("removeById")
    @Operation(summary = "根据id删除公寓信息")
    public Result removeById(@RequestParam Long id){
        service.removeApartmentById(id);
        return Result.ok();
    }
}
